package com.whsxt.common.util;

import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import com.whsxt.common.CachePool;
import com.whsxt.sys.data.DeptData;
import com.whsxt.sys.data.UserData;
import com.whsxt.sys.domain.Dept;
import com.whsxt.sys.domain.User;
import cn.hutool.core.bean.BeanUtil;

@Aspect
@Component
@EnableAspectJAutoProxy
public class CacheAspectUtil {
	
	private Log log = LogFactory.getLog(CacheAspectUtil.class);
	
	private static Map<String, Object> map = CachePool.map;
	
	private static String PREFIX_DEPT = "dept";
	
	private static String PREFIX_USER = "user";
	
	/**
	 * 部门删除缓存
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* com.whsxt.sys.service.impl.DeptServiceImpl.removeById(..))")
	public Object deptDelete(ProceedingJoinPoint joinPoint) throws Throwable {
		Integer id = (Integer) joinPoint.getArgs()[0];
		Boolean success = (Boolean) joinPoint.proceed();
		log.info("部门数据库已删除id="+id+"的数据");
		if(success) {
			map.remove(PREFIX_DEPT+id);
			log.info("缓冲已删除部门id="+id+"的数据");
		}
		return success;
	}
	/**
	 * 部门 更新方法 缓存
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* com.whsxt.sys.service.impl.DeptServiceImpl.updateById(..))")
	public Object deptUpdate(ProceedingJoinPoint joinPoint) throws Throwable {
		DeptData data = (DeptData) joinPoint.getArgs()[0];
		Boolean success = (Boolean) joinPoint.proceed();
		log.info("部门库已跟新id="+data.getId()+"的数据");
		if(success) {
			 Dept dept = (Dept) map.get(PREFIX_DEPT+data.getId());
			 if(dept == null) {
				 dept = new Dept();
			 } 	
			 BeanUtil.copyProperties(data, dept);
			 map.put(PREFIX_DEPT+dept.getId(), dept);
			 log.info("缓冲已跟新部门id="+data.getId()+"的数据");
		}
		return success;
	}
	/**
	 * 部门查询方法 缓冲
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* com.whsxt.sys.service.impl.DeptServiceImpl.getById(..))")
	public Object cacheDeptGet(ProceedingJoinPoint joinPoint) throws Throwable {
		//取出第一个参数
		Integer object = (Integer) joinPoint.getArgs()[0];
		//从缓存里面取
		Object res1 = map.get(PREFIX_DEPT+object);
		if(res1!=null) {
			log.info("缓冲中有部门id="+object+"的数据");
			return res1;
		}else {
			Dept res2 = (Dept) joinPoint.proceed();
			log.info("缓冲中没有部门id="+object+"的数据,从数据库查");
			map.put(PREFIX_DEPT+res2.getId(), res2);
			return res2;
		}
	}
	
	@Around(value = "execution(* com.whsxt.sys.service.impl.DeptServiceImpl.save(..))")
	public Object cacheDeptInsert(ProceedingJoinPoint joinPoint) throws Throwable {
		DeptData data = (DeptData) joinPoint.getArgs()[0];
		Boolean success = (Boolean) joinPoint.proceed();
		 log.info("部门数据库已保存id="+data.getId()+"的数据");
		if(success) {
			 Dept dept = (Dept) map.get(PREFIX_DEPT+data.getId());
			 if(dept == null) {
				 dept = new Dept();
			 }
			 BeanUtil.copyProperties(data, dept);
			 map.put(PREFIX_DEPT+dept.getId(), dept);
			 log.info("缓冲已保存部门id="+dept.getId()+"的数据");
		}
		return success;
	}
	
	
	
	/**
	 * 用户删除缓存
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* com.whsxt.sys.service.impl.UserServiceImpl.removeById(..))")
	public Object userDelete(ProceedingJoinPoint joinPoint) throws Throwable {
		Integer id = (Integer) joinPoint.getArgs()[0];
		Boolean success = (Boolean) joinPoint.proceed();
		log.info("用户数据库已删除id="+id+"的数据");
		if(success) {
			map.remove(PREFIX_USER+id);
			log.info("缓冲已删除用户id="+id+"的数据");
		}
		return success;
	}
	/**
	 * 用户跟新 更新方法 缓存
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* com.whsxt.sys.service.impl.UserServiceImpl.updateById(..))")
	public Object userUpdate(ProceedingJoinPoint joinPoint) throws Throwable {
		User data = (User) joinPoint.getArgs()[0];
		Boolean success = (Boolean) joinPoint.proceed();
		log.info("用户库已跟新用户id="+data.getId()+"的数据");
		if(success) {
			 User user = (User) map.get(PREFIX_USER+data.getId());
			 if(user == null) {
				 user = new User();
			 } 	
			 BeanUtil.copyProperties(data, user);
			 map.put(PREFIX_USER+user.getId(), user);
			 log.info("缓冲已跟新用户id="+data.getId()+"的数据");
		}
		return success;
	}
	/**
	 * 用户查询方法 缓冲
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* com.whsxt.sys.service.impl.UserServiceImpl.getById(..))")
	public Object cacheUserGet(ProceedingJoinPoint joinPoint) throws Throwable {
		//取出第一个参数
		Integer object = (Integer) joinPoint.getArgs()[0];
		//从缓存里面取
		Object res1 = map.get(PREFIX_USER+object);
		if(res1!=null) {
			log.info("缓冲中有用户id="+object+"的数据");
			return res1;
		}else {
			User res2 = (User) joinPoint.proceed();
			log.info("缓冲中没有用户id="+object+"的数据,从数据库查");
			map.put(PREFIX_USER+res2.getId(), res2);
			return res2;
		}
	}
	/**
	 *  插入用户 
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* com.whsxt.sys.service.impl.UserServiceImpl.save(..))")
	public Object cacheUserInsert(ProceedingJoinPoint joinPoint) throws Throwable {
		UserData data = (UserData) joinPoint.getArgs()[0];
		Boolean success = (Boolean) joinPoint.proceed();
		 log.info("用户数据库已保存id="+data.getId()+"的数据");
		if(success) {
			 User user = (User) map.get(PREFIX_DEPT+data.getId());
			 if(user == null) {
				 user = new User();
			 }
			 BeanUtil.copyProperties(data, user);
			 map.put(PREFIX_DEPT+user.getId(), user);
			 log.info("缓冲已保存用户id="+user.getId()+"的数据");
		}
		return success;
	}
}
